<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 微博用户认证管理
 * @author	Baiyu	2016-12-24
 */
// TODO 完善编码规范
class AdminVerifyController extends AdminbaseController {
	
	/**
	 * 首页
	 * @author	Baiyu	2016-12-24
	 */
	public function index(){
//		$this->display();
		$this->applyList();
	}
	
	/**
	 * 认证申请列表
	 * @author	zhangwei	2016-12-26
	 * @param	string		$status			要列出哪个状态的申请 approved已审核|pending待审核
	 */
	public function applyList($status = 'pending'){
		//TODO 根据传入的状态查出对应的列表
		//TODO 要求可以按用户ID、认证类型搜索
		//TODO 要求可分页,并且搜索结果也可分页
		$weibo_verify = D('WeiboVerifyApply');
		$status == 'pending'  && $where['approved'] = 0;
		$status == 'approved' && $where['approved'] = 1;
		$status == 'rebut'	  && $where['approved'] = 2;
		$search=array(
			'user_id'   => I('user_id'),
			'type_name' => I('type_name')
		);
		$search['user_id'] && $where['user_id'] = $search['user_id'];
		// 正则匹配输入的认证类型
		if($search['type_name']){
			strpos("/个人/",  $search['type_name']) !== false  && $where_type[] = 1;
			strpos("/机构/",  $search['type_name']) !== false  && $where_type[] = 2;
			strpos("/主持人/", $search['type_name']) !== false  && $where_type[] = 3;
			if($where_type){
				$where['type'] = array('in',$where_type);
			}
		}
		//分页查询
		$count = $weibo_verify -> where($where) -> count();
		$page  = $this -> page($count,10);
		$show  = $page->show("Admin");// 分页显示输出
		$weibo_list = $weibo_verify
			->where($where)
			->limit($page->firstRow.','.$page->listRows)
			->order('orderby asc')
			->select();
		$this -> assign('list', $weibo_list);
		$this -> assign('page',	 $show); //todo 分页样式的问题显示不正常
		$this -> assign('status',$status);
		$this -> assign('search',$search);
		$this -> display('index');
	}
	
	/**
	 * 审核认证申请
	 * @author	zhangwei	2016-12-24
	 */
	public function audit(){
		// 修改审核申请表
		// 修改微博用户表的认证状态和用户说明,审核前该字段为用户简介,审核后为认证说明
		// 通过后推送消息
		$weibo_verify = D('WeiboVerifyApply');
		$weibo_user   = D('WeiboUser');
		$apply_id     = I('apply_id');
//		$user_id	  = $this -> user_id;
		$user_id      = I('user_id');
		$weibo_info	  = $weibo_verify -> find($apply_id);
		$data1 = array(
			'approved' 	 =>1,
			'audit_time' => time()
		);
		$result1      = $weibo_verify -> where('apply_id='.$apply_id) -> save($data1);
		$data2 =array(
			'verify'	   => $weibo_info['type'],
			'introduction' => $weibo_info['apply_name']
		);
		$result2 = $weibo_user -> where('user_id='.$user_id)->save($data2);
		if ($result1 && $result2){
			$res = \Push\Lib\PushLib::push_message('身份认证通知','审核成功',array('type'=>'system_message','event' => 'weibo_auth','notice_id' => 4, 'status' => 1),10);
			$this->success('审核成功');
		}else{
			$this->error('审核失败');
		}
	}
	
	/**
	 * 认证资料详情页面
	 * @author  zhangwei   2016-12-26
	 */
	public function detail(){
		$apply_id     = I('apply_id');
		$weibo_verify = D('WeiboVerifyApply');
		$result       = $weibo_verify->find($apply_id);
		$this -> assign('info_detail',$result);
		$this -> display();
	}
	
	/**
	 *  删除申请还是重新填写不对的字段(弃用的方法)
	 * @author  zhangwei   2016-12-27
	 */
	public function delete(){
		//TODO @Baiyu 拒绝机制?删除时推送拒绝消息?
		$apply_id = I('apply_id');
		$user_id = I('user_id');
		$weibo_verify = D('WeiboVerifyApply');
		$apply_info = $weibo_verify -> find($apply_id);
		$result = $weibo_verify -> delete($apply_id);
		if ($result){
			$simg1 = C('UPLOADPATH').$apply_info['multiclass_photo'];
			$simg2 = C('UPLOADPATH').$apply_info['handheld_photo'];
			$simg3 = C('UPLOADPATH').$apply_info['identity_photo'];
			$simg4 = C('UPLOADPATH').$apply_info['credential4'];
			@unlink($simg1);
			@unlink($simg2);
			@unlink($simg3);
			@unlink($simg4);
			$this ->success('审核失败了');
			// push_message($user_id,'审核失败了',array('type'=>'weibo_auth'));
			return;
		}
		
	}
	
	/**
	 * 认证驳回的方法
	 * @author  zhangwei 2017-01-03
	 */
	public function  rebut(){
		$user_id  = I('user_id');
		if(IS_POST){
			$weibo_verify 		= D('WeiboVerifyApply');
			$refuse_reason 		= json_encode(I('refuse_reason'));
			$reason_description = I('content');  //原因描述,推送功能用
			// tood 多选框怎么验证son
			if( $weibo_verify->create() ){
				$data =array(
					'refuse_reason' =>$refuse_reason,
					'approved'		=>2
				);
				$result = $weibo_verify -> where('user_id='.$user_id) ->save($data);
				if($result){
					$res = \Push\Lib\PushLib::push_message('身份认证通知',$reason_description,array('type'=>'system_message','event' => 'weibo_auth','notice_id' => 4,'status' => 2),$user_id);
					$arr = array('msg' => '提交成功');
					$this->ajaxReturn($arr);
				}else{
					$arr = array('msg' =>'提交失败');
					$this->ajaxReturn($arr);
				}
			}else{
				$res= $weibo_verify->getError();
				$this->ajaxReturn($res);
			}
		}else{
			$this->assign('user_id',$user_id);
			$this->display();
		}
	}
	
	public function indexBat($type = 'order'){
		$weibo_verify = D('WeiboVerifyApply');
		if($type == 'order'){
			$array_orderby = I('post.orderby');
			if($weibo_verify ->saveBatch($array_orderby,'apply_id','orderby') !== false){
				$this -> success('排序成功');
			}else{
				$this -> error('排序失败');
			}
		}else if($type == 'delete'){
			$ids = I('post.ids');
			if($weibo_verify -> delete(implode(',',$ids))){
				$this -> success('删除成功');
			}else{
				$this -> error('删除失败');
			}
		}
	}
}